package com.jse.io.ObjectStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class ObjectOutputStreamTest {

	@Test
	public void testWrite() {
		try {
			// Store Serialized User Object in File
			FileOutputStream fileOutputStream = new FileOutputStream(
					"G:/opensource/coding/re-javase/src/main/java/com/jse/io/ObjectStream/user.txt");
			User user = new User("Anirudh", "lastName", "email");
			// 主要用于网络传输
			ObjectOutputStream output = new ObjectOutputStream(fileOutputStream);
			output.writeObject(user);
			output.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testRead() {
		try {
			FileInputStream fileInputStream = new FileInputStream(
					"G:/opensource/coding/re-javase/src/main/java/com/jse/io/ObjectStream/user.txt");
//			User user = new User("Anirudh", "lastName", "email");
			// 主要用于网络传输
			ObjectInputStream input = new ObjectInputStream(fileInputStream);
			User user = (User) input.readObject();
			System.out.println(user.toString());
			input.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
